package com.ecdc.soexample;

import android.app.Activity;
import android.bluetooth.BluetoothAdapter;
import android.bluetooth.BluetoothDevice;
import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

import com.ecdc.annotation.BindViewUtils;
import com.ecdc.annotation.EcdcAnnotation;
import com.deep_tools.ecdc.R;
import org.w3c.dom.Text;

import java.util.ArrayList;
import java.util.List;

import adapter.BluetoothDeviceAdapter;
import bluetooth.Utils;
import utils.OtherUtil;

public class PairWatchActivity extends Activity {
    @EcdcAnnotation.EcdcAnno(R.id.listView_activity_pair_watch)
    private ListView listView_activity_pair_watch;
    @EcdcAnnotation.EcdcAnno(R.id.customize_activity_pair_watch)
    private TextView customize_activity_pair_watch;
    @EcdcAnnotation.EcdcAnno(R.id.searching_activity_pair_watch)
    private TextView searching_activity_pair_watch;



    private BluetoothAdapter mBluetoothAdapter;
    private BluetoothAdapter.LeScanCallback mLeScanCallback;
    private BluetoothDeviceAdapter mBluetoothDeviceAdapter;
    private int STARSCAN=1,STOPSCAN=0;//表示开始扫描蓝牙设备


    private Handler handler = new Handler() {
        @Override
        public void handleMessage( Message msg) {
            super.handleMessage(msg);
            switch (msg.what) {
                case 0://开始绑定服务
                    // 绑定服务
//                    Intent gattServiceIntent = new Intent(HomeActivity.this, BluetoothLeService.class);
//                    bindService(gattServiceIntent, mServiceConnection, BIND_AUTO_CREATE);

                    break;
                case 1://初始化蓝牙并开始扫描蓝牙设备
//                    initBL();
                    break;
                case 2:
//                    starOrStopScanBluetoothDevice(STOPSCAN);
//                    handler.removeMessages(3);

                    break;
                case 3:
                    String content=searching_activity_pair_watch.getText().toString();
                    int oldLength=content.length();

                    if(oldLength==0){
                        searching_activity_pair_watch.setText(".");
                    }else if(oldLength==1){
                        searching_activity_pair_watch.setText("..");

                    }else if(oldLength==2){
                        searching_activity_pair_watch.setText("...");

                    }else if(oldLength==3){
                        searching_activity_pair_watch.setText("");

                    }
                    handler.sendEmptyMessageDelayed(3,400);

                    break;
                default:
                    break;

            }


        }
    };

    @Override
    protected void onCreate( Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_pair_watch);
        try {
            BindViewUtils.getInstance().initEcdcAnnotationActivity(this,this);
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        }

        initBL();
        onClickL();
        searching_activity_pair_watch.setText("");

        handler.sendEmptyMessage(3);

        //设置litview拖动到边缘是的颜色
        try {
            OtherUtil.setEdge(this,listView_activity_pair_watch,Color.rgb(239,239,239));
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    private void onClickL(){
        customize_activity_pair_watch.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                PairWatchActivity.this.finish();
            }
        });

        listView_activity_pair_watch.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {

                Intent intent=new Intent(PairWatchActivity.this,PairingWatchActivity.class);
                TextView bluetoothDeviceAddress=view.findViewById(R.id.bluetoothDeviceAddress);
                intent.putExtra("bluetoothDeviceAddress",bluetoothDeviceAddress.getText().toString());
                startActivity(intent);
                PairWatchActivity.this.finish();
            }
        });
    }

    /**
     * 初始化蓝牙
     */
    public void initBL() {
        if (Utils.isSupportBle() && Utils.isOpenBlueTooth()) {
//            handler.sendEmptyMessageDelayed(2, 3600);//3秒后停止扫描蓝牙设备
            starOrStopScanBluetoothDevice(STARSCAN);
        } else {
            Toast.makeText(this, "蓝牙设备不支持或者蓝牙未打开", Toast.LENGTH_SHORT).show();
        }
    }
    /**
     * 扫描蓝牙设备
     */
    @SuppressWarnings("deprecation")
    public void starOrStopScanBluetoothDevice(int flag) {
        mBluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
        if(mBluetoothDeviceAdapter==null){
            List<BluetoothDevice> listBluetoothDevice=new ArrayList<>();

            mBluetoothDeviceAdapter = new BluetoothDeviceAdapter(listBluetoothDevice);
            listView_activity_pair_watch.setAdapter(mBluetoothDeviceAdapter);
        }
        if (mLeScanCallback == null) {
            BluetoothAdapter.LeScanCallback mLeScanCallback = new BluetoothAdapter.LeScanCallback() {

                @Override
                public void onLeScan(final BluetoothDevice device, int rssi, byte[] scanRecord) {
                    String btAddress = device.getAddress();
                    String btName = device.getName()+"";
//                    if(btName.equalsIgnoreCase("hw31")){
                    if(true){

                        mBluetoothDeviceAdapter.addDevice(device);
                        mBluetoothDeviceAdapter.updateBLDeviceList();
                    }


                }
            };
            this.mLeScanCallback = mLeScanCallback;
        }
        if (STARSCAN == flag) {
            mBluetoothAdapter.startLeScan(mLeScanCallback);
        } else if (STOPSCAN == flag) {
            mBluetoothAdapter.stopLeScan(mLeScanCallback);
        }
    }


}
